import Vue from 'vue'
import Vuex from 'vuex'
import { getUserInfo } from '@/api'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken (state, newToken) {
      state.token = newToken
    },
    setUserInfo (state, userinfo) {
      state.userInfo = userinfo
    }
  },
  actions: {
    async userInfoHandle ({ commit }) {
      const res = await getUserInfo()
      commit('setUserInfo', res.data.data)
    }
  }
})
